<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-07-02 09:50:36
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-09-18 09:22:17
 * @FilePath: \linePatrol\vue-element-plus-admin\src\views\EquipManage\EquipLedger\components\Edit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="tsx">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, ref, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { EquipLedgerHooks } from '../../FacOutComponents/LedgerHooks'
import { PipePointInfoType } from '@/api/gis/type'
import { useEquipStore } from '@/store/modules/equip'
const { required } = useValidator()
const { Formschema } = EquipLedgerHooks()
const EquipStore = useEquipStore()
const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods
const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<PipePointInfoType>>,
    default: () => null
  },
  FormDisabled: {
    type: Boolean as PropType<boolean>,
    default: () => null
  }
})
// 可修改的响应式表单数据和配置
const CustomizeForm = ref([...Formschema])

watch(
  () => EquipStore.getFacilityByFacId,
  async (currentRow) => {
    if (!currentRow) return
    CustomizeForm.value = [...Formschema, ...currentRow.formschema]
    const formData = await getFormData()
    if (formData.faciId == props.currentRow?.faciId && props.currentRow?.customize) {
      const res = EquipStore.setFacilityByTemplate(props.currentRow?.customize)
      EquipStore.setEquipFormData({ ...formData, ...res?.current })
    } else {
      EquipStore.setEquipFormData({ ...formData, ...currentRow.current })
    }
  },
  {
    deep: true,
    immediate: true
  }
)

const rules = reactive({
  faciId: [required()],
  facState: [required()],
  facName: [required()],
  operationDate: [required()],
  redType: [required()],
  isCoAs: [required()]
})

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const data = await getFormData()
    data.file = data.file.map((item) => {
      if (item.response) {
        return item.response.data
      } else {
        return item
      }
    })
    const formData = JSON.parse(JSON.stringify(data))
    formData.file = JSON.stringify(formData.file)
    if (EquipStore.getFacilityByFacId) {
      const formConf = EquipStore.getFacilityByFacId.template.formConf
      const i = EquipStore.getFacilityByFacId.template.drawingList.map((item) => {
        item.defaultValue = formData[item.vModel]
        return item
      })
      formData['customize'] = JSON.stringify({ drawingList: i, formConf: formConf })
    }
    return formData
  }
}
watch(
  () => EquipStore.EquipFormData,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form
    :disabled="props.FormDisabled"
    :rules="rules"
    @register="formRegister"
    :schema="CustomizeForm"
  />
</template>
<style scoped>
:deep(.el-col) {
  padding: 10px 15px 5px 0px !important;
}
/* #4099f5 */
:deep(.el-divider--horizontal) {
  border-top: 1px #4099f5 solid !important;
}
:deep(.el-divider__text) {
  color: #4099f5;
}
</style>
@/api/gis/type
